package com.cc.openeye.dynamic.recommend

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import com.cc.openeye.dynamic.recommend.items.RecommendDynamicItemHelper
import com.cc.openeye.dynamic.recommend.items.banner.BannerItem
import com.cc.openeye.dynamic.recommend.items.dynamic.DynamicItem
import com.cc.openeye.dynamic.recommend.items.loadMore.LoadMoreComponent
import com.cc.openeye.dynamic.recommend.items.loadMore.PreLoadMoreComponent
import com.cc.openeye.dynamic.recommend.items.square.SquareItem
import com.tencent.kuikly.compose.foundation.layout.Arrangement
import com.tencent.kuikly.compose.foundation.layout.Box
import com.tencent.kuikly.compose.foundation.layout.fillMaxHeight
import com.tencent.kuikly.compose.foundation.layout.fillMaxWidth
import com.tencent.kuikly.compose.foundation.layout.padding
import com.tencent.kuikly.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import com.tencent.kuikly.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import com.tencent.kuikly.compose.foundation.lazy.staggeredgrid.StaggeredGridItemSpan
import com.tencent.kuikly.compose.foundation.lazy.staggeredgrid.items
import com.tencent.kuikly.compose.foundation.lazy.staggeredgrid.rememberLazyStaggeredGridState
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.platform.LocalActivity
import com.tencent.kuikly.compose.ui.unit.dp
import com.tencent.kuikly.core.coroutines.launch
import com.tencent.kuikly.core.pager.Pager

@Composable
fun RecommendComponent() {

    val dynamicViewModel = remember {
        RecommendViewModel.newInstance()
    }

    val localPager = LocalActivity.current.getPager() as Pager
    fun requestDynamicData() {

        localPager.lifecycleScope.launch {
            dynamicViewModel.requestDynamicData()
        }
    }

    Box(modifier = Modifier.fillMaxWidth().fillMaxHeight()) {
        LazyVerticalStaggeredGrid(
            columns = StaggeredGridCells.Fixed(2),
            modifier = Modifier
                .padding(start = 15.dp, end = 15.dp, top = 15.dp)
                .fillMaxWidth()
                .fillMaxHeight(),
            verticalItemSpacing = 8.dp,
            horizontalArrangement = Arrangement.spacedBy(8.dp),
            state = rememberLazyStaggeredGridState(),
        ) {
            items(
                dynamicViewModel.datas,
                span = { item -> item.getStaggeredGridItemSpan() }) { item ->
                when (item.getItemType()) {
                    RecommendDynamicItemHelper.ITEM_TYPE_SQUARE -> {
                        SquareItem(item.data?.itemList ?: emptyList())
                    }

                    RecommendDynamicItemHelper.ITEM_TYPE_BANNER -> {
                        BannerItem(item.data?.itemList ?: emptyList())
                    }

                    RecommendDynamicItemHelper.ITEM_TYPE_DYNAMIC -> {
                        if (item.data != null) {
                            DynamicItem(item.data)
                        }
                    }

                    RecommendDynamicItemHelper.ITEM_TYPE_PRE_LOAD_MORE -> {
                        PreLoadMoreComponent {
                            requestDynamicData()
                        }
                    }
                }
            }

            item(span = StaggeredGridItemSpan.FullLine) {
                LoadMoreComponent(dynamicViewModel.loadMoreStatus.value) {
                    requestDynamicData()
                }
            }

        }
    }
}
